﻿using DimensionsHelper.Client.Navigation;
using DimensionsHelper.Client.Services;
using DimensionsHelper.Client.ViewModels;
using DimensionsHelper.Client.ViewModels.CsvFormatter;
using DimensionsHelper.Client.ViewModels.DataCollector;
using DimensionsHelper.Client.ViewModels.Message;
using DimensionsHelper.Client.ViewModels.TaskPanel;
using DimensionsHelper.Client.Views;
using DimensionsHelper.Client.Views.CsvFormatter;
using DimensionsHelper.Client.Views.DataCollector;
using DimensionsHelper.Common.Contracts.TableFormatter;
using DimensionsHelper.Common.Contracts.DataCollector;
using DimensionsHelper.Common.Contracts.Excel;
using DimensionsHelper.Common.Contracts.Services;
using DimensionsHelper.Common.Contracts.Sql;
using DimensionsHelper.Common.Contracts.Table;
using DimensionsHelper.Services.TableFormatter;
using DimensionsHelper.Services.CsvReader;
using DimensionsHelper.Services.DataCollector;
using DimensionsHelper.Services.Excel.EPPlus;
using DimensionsHelper.Services.SQL;
using Microsoft.Extensions.Hosting;

namespace DimensionsHelper.Client;

public partial class App
{
    private static readonly IHost ServiceHost = Host.CreateDefaultBuilder()
        .ConfigureServices((_, services) =>
        {
            services.AddSingleton<NavigationService>();
            services.AddSingleton<IMessageService, MessageService>();
            services.AddSingleton<IFilePikerService, FilePickerService>();
            services.AddSingleton<IFolderPickerService, FolderPickerService>();

            services.AddTransient<FileInfoViewModel>();
            services.AddTransient<IndexFileViewModel>();
            services.AddTransient<MessageViewModel>();
            services.AddTransient<TaskViewModel>();
            services.AddTransient<RangeStyleOptionsViewModel>();
            services.AddTransient<FontStyleOptionsViewModel>();

            services.AddSingleton<TaskListViewModel>();
            services.AddSingleton<IndexFileListViewModel>();
            services.AddSingleton<DataCollectorViewModel>();
            services.AddSingleton<CsvFileGroupListViewModel>();
            services.AddSingleton<SigComparisonMapViewModel>();
            services.AddSingleton<SigCharOptionsViewModel>();
            services.AddSingleton<TableStyleOptionsViewModel>();
            services.AddSingleton<MenuOptionsViewModel>();
            services.AddSingleton<CsvFormatterViewModel>();
            services.AddSingleton<MainWindowViewModel>();

            services.AddSingleton<StringPairPage>();
            services.AddSingleton<IndexFileListPage>();
            services.AddSingleton<DataCollectorPage>();
            services.AddSingleton<CsvFileGroupListPage>();
            services.AddSingleton<TableStyleOptionsPage>();
            services.AddSingleton<MenuOptionsPage>();
            services.AddSingleton<CsvFormatterPage>();
            services.AddSingleton<MainWindow>();

            // Keyed singleton
            foreach (NavigationTargetData data in NavigationTargetSource.Instance.KeyedNavigationTargets)
            {
                if (data.ModelType != null)
                {
                    services.AddKeyedSingleton(data.ModelType, data.Key, data.ModelType);
                }

                if (data.PageType != null)
                {
                    services.AddKeyedSingleton(data.PageType, data.Key, data.PageType);
                }
            }

            services.AddTransient<IExcelAggregateWriter, ExcelAggregateWriter>();
            services.AddTransient<IExcelSummaryWriter, ExcelSummaryWriter>();
            services.AddTransient<IExcelWorkbookOperator, ExcelWorkbookOperator>();
            services.AddTransient<ITableReader, CsvStreamReader>();
            services.AddTransient<IUpdateSqlBuilder, UpdateSqlBuilder>();
            services.AddTransient<IInsertSqlBuilder, InsertSqlBuilder>();
            services.AddTransient<ITableFormatter, TableFormatter>();
            services.AddTransient<ICollector, ExcelDataCollector>();
        })
        .Build();


    public static T? GetService<T>()
    {
        return ServiceHost.Services.GetService<T>();
    }


    [STAThread]
    public static void Main()
    {
        ServiceHost.Start();
        App app = new();
        app.InitializeComponent();
        app.MainWindow = ServiceHost.Services.GetRequiredService<MainWindow>();
        app.MainWindow.Visibility = Visibility.Visible;
        app.Run();
    }
}